Java From Zero: Learn Java Programming Fast for Beginners to Professionals: The Complete Guide With Code Examples and Exercises to Become a Professional by Scott Brandt
Author:Scott Brandt [Brandt, Scott]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2023-02-11T00:00:00+00:00
Implementing Polymorphism in Java and its Relation to Inheritance
Polymorphism in Java establishes that a method can be executed in different forms. Suppose you have the interface "geometric figure." When you have this situation, you are referring to all geometric figures, and you will implement methods that enable you to calculate their different areas. This means you will have the "square method," "circle method," "triangle method," and so on. This means that the class will still be the same, but the way each of the methods behaves is different because each geometric figure will be calculated distinctly.
Polymorphism is directly related to inheritance since it will enable the inheritance of the attributes and methods from another class. When you apply polymorphism to this, you are using these methods to perform different tasks, enabling you to do the same thing in different ways.
Pro tip: When you use the word "final" at the end of methods and classes, you are declaring that they cannot be reimplemented or extended. For example, all of those pertaining to the String method and even the String classes are considered final.
class GeometricFigure{ public void shapeColor() {
System.out.println("The figure is");
}
} class Square extends GeometricFigure {
public void shapeColor() {
System.out.println("The square is red"); }
}
class Circle extends GeometricFigure { public void shapeColor() {
System.out.println("The circle is blue");
} }
public class Main {
public static void main(String[] args) { GeometricFigure mySquare = new Square(); // Create a Square object
GeometricFigure myCircle = new Circle(); // Create a Circle object
mySquare.shapeColor(); myCircle.shapeColor();
} The output for this program will be:
The square is red
The circle is blue As you will see, we have created two different shape colors, one for the square and red, and one for the circle and blue.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(11922)
Hello! Python by Anthony Briggs(9388)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9354)
The Mikado Method by Ola Ellnestam Daniel Brolund(9321)
Dependency Injection in .NET by Mark Seemann(8867)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7861)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7353)
Grails in Action by Glen Smith Peter Ledbrook(7305)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7127)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5969)
Kotlin in Action by Dmitry Jemerov(4654)
Practical Vim (for Kathryn Amaral) by Drew Neil(3734)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3602)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3452)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3253)
Learning Java by Patrick Niemeyer & Daniel Leuck(2875)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2850)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2847)
Microservices with Go by Alexander Shuiskov(2645)